package com.wwwarehouse.contract.orders;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;

import com.tencent.smtt.export.external.interfaces.WebResourceError;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.tools.ToastUtils;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * 通用webView
 * Created by ShineF on 2017/9/21 0021.
 */

public class CommonWebViewFragment extends BaseFragment {

    private WebView mWebView;
    private String mData;
    private StateLayout mStateLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_common_web_view, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mWebView = $(R.id.x5_web_view);
        mStateLayout = $(R.id.sl_state);
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {
        mWebView.setVisibility(View.INVISIBLE);
        mStateLayout.setVisibility(View.VISIBLE);
        mStateLayout.showProgressView(true);
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        mData = bundle.getString("data");
        WebSettings mWebSetting = mWebView.getSettings();
        mWebSetting.setSaveFormData(false);
        mWebSetting.setJavaScriptEnabled(true);
        mWebSetting.setSupportZoom(false);
        mWebSetting.setDefaultTextEncodingName("UTF-8");
        mWebSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebSetting.setUseWideViewPort(true);
        mWebSetting.setLoadWithOverviewMode(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                return super.shouldOverrideUrlLoading(webView, s);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
//                mStateLayout.showContentView();
            }

            @Override
            public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
            }
        });



        mWebView.addJavascriptInterface(new AndroidWebInterface(), "android");
    }

    private class AndroidWebInterface {

        /**
         * 显示progress
         */
        @JavascriptInterface
        public void showProgress() {
            showProgressDialog();

        }

        /**
         * 隐藏progress
         */
        @JavascriptInterface
        public void hideProgress() {
            mWebView.setVisibility(View.VISIBLE);
            mStateLayout.setVisibility(View.INVISIBLE);
            mStateLayout.showContentView();
            dismissProgressDialog();
        }

        /**
         * 获取token
         *
         * @return token
         */
        @JavascriptInterface
        public String getToken() {
            return sp.getValue("sp_token");
        }

        /**
         * 跳转到Login
         */
        @JavascriptInterface
        public void gotoLogin() {
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
//            pushFragment(new TempCardFragment());
        }

        @JavascriptInterface
        public void showErrorMsg(String msg) {

        }

        /**
         * 获取Javascript需要请求的数据, json格式
         *
         * @return json
         */
        @JavascriptInterface
        public String getData() {
            return mData;
        }


        // TODO: 2017/9/21 0021  根据需求不同去添加与前端约束好的方法

        /**
         * 订单监控
         */
        @JavascriptInterface
        public void reponseOrdersMonitorClick() {
            ToastUtils.showToast("@Javascript");
        }
    }

}
